package com.example.knowledgeget1.Util;

import com.example.knowledgeget1.GraphDBCommon.Entity.Node;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.io.IOException;
import java.util.*;

public class NodeResponseLexer {

    public static void Lexer(String nodeName, String jsonData) throws IOException {
        // 解析JSON数据
        JSONArray jsonArray = new JSONArray(jsonData);
        for(int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            Node newNode = new Node();
            newNode.setName(nodeName);

            // 从JSON对象中提取信息
            Map<String, String> parsedContent = extractContent(jsonObject);

            // 构建node节点
            for (Map.Entry<String, String> entry : parsedContent.entrySet()) {
                setNodeProperties(newNode, entry);
            }

            // 创建RestTemplate实例并发送POST请求
            sendPostRequest(newNode);
        }
    }

    private static Map<String, String> extractContent(JSONObject jsonObject) {
        Map<String, String> content = new LinkedHashMap<>();
        content.put("introduce", jsonObject.optString("介绍"));
        content.put("example", jsonObject.optString("代码示例"));
        content.put("function", jsonObject.optString("作用"));
        content.put("grammar", jsonObject.optString("语法"));
        content.put("exercise", jsonObject.optString("练习题"));
        content.put("characteristic", jsonObject.optString("特点"));
        content.put("recommendation", jsonObject.optString("推荐资料"));

        return content;
    }

    private static void setNodeProperties(Node newNode, Map.Entry<String, String> entry) {
        switch (entry.getKey()) {
            case "introduce":
                newNode.setIntroduce(entry.getValue());
                break;
            case "example":
                newNode.setExample(entry.getValue());
                break;
            case "function":
                newNode.setFunction(entry.getValue());
                break;
            case "grammar":
                newNode.setGrammar(entry.getValue());
                break;
            case "exercise":
                newNode.setExercise(entry.getValue());
                break;
            case "characteristic":
                newNode.setCharacteristic(entry.getValue());
                break;
            case "recommendation":
                newNode.setRecommendation(entry.getValue());
                break;
        }
    }

    private static void sendPostRequest(Node newNode) {
        // 创建RestTemplate实例
        RestTemplate restTemplate = new RestTemplate();

        // 设置请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        // 构建请求体
        HttpEntity<Node> entity = new HttpEntity<>(newNode, headers);

        // 发送POST请求
        String url = "http://localhost:8080/nodes/saveNode";
        restTemplate.postForEntity(url, entity, Node.class);

    }

}
